// mock/user.ts
import LogSingleton from './log';
let createUserList = [
	{
		userId: 1,
		avatar: 'https://pic1.zhimg.com/80/v2-083faf550543c1e9f134b56b3322ee3c_720w.webp',
		userName: 'admin',
		password: '123456',
		tle: '18247847130',
		desc: '下船不谈船里事',
		roles: ['admin'],
		buttons: ['cuser.detail', 'sys:role:add'],
		routes: ['home'],
		token: 'Admin Token',
		describe: 0, //是否可以删除
	},
	{
		userId: 2,
		avatar: 'https://pic1.zhimg.com/80/v2-e1427f6a21122ac163ff98d24f55d372_720w.webp',
		userName: 'system',
		password: '123456789',
		tle: '0',
		desc: '旧人不谈近况，新人不讲过往',
		roles: ['user'],
		buttons: ['cuser.detail', 'cuser.user'],
		routes: ['user'],
		token: 'Admin Token',
		describe: 1,
	},
];
let num = 3;

export default [
	// 用户登录接口
	{
		url: '/api/user/login',
		method: 'post',
		response: (request: any) => {
			LogSingleton.addToLogData({
				logName: '登录',
				url: request.url,
				data: request.body,
			});

			// 获取请求体携带过来的用户名与密码
			const { userName, password, code, type } = request.body;
			console.log(request);
			// 调用获取用户信息函数，用于判断是否有此用户
			let checkUser: any = '';
			if (type == 2) {
				checkUser = createUserList.find((item) => item.tle === userName);
			} else {
				checkUser = createUserList.find((item) => item.userName === userName && item.password === password);
			}

			// 没有用户则返回失败信息
			if (!checkUser) {
				return {
					code: 201,
					data: {
						message: '账号或者密码不正确',
					},
				};
			}
			// 如果有返回成功信息
			console.log(checkUser);

			return {
				code: 200,
				data: {
					...checkUser,
				},
			};
		},
	},
	//获取全部用户信息
	{
		url: '/api/user/getAllUser',
		method: 'post',
		response: (request: any) => {
			// 获取请求头携带的 token
			const token = request.headers.token;
			let newList: any = [];
			if (request.body.userName) {
				newList = createUserList.filter((item) => item.userName.indexOf(request.body.userName) != -1);
			} else {
				newList = createUserList;
			}

			return {
				code: 200,
				data: newList,
			};
		},
	},
	//新增用户 / 修改
	{
		url: '/api/user/addUser',
		method: 'post',
		response: (data: any) => {
			LogSingleton.addToLogData({
				logName: '新增用户',
				url: data.url,
				data: data.body,
			});
			if (!data.body.userName) {
				return {
					code: 400,
					data: {
						message: '请输入用户名称',
					},
				};
			}
			if (data.body.userId == '') {
				//新增
				data.body.userId = num++;
				createUserList.push(data.body);
			} else {
				//修改
				let idx = -1;
				createUserList.map((item, index) => {
					if (item.userId == data.body.userId) {
						// item = data.body;
						idx = index;
					}
				});
				if (idx != -1) {
					createUserList[idx] = data.body;
				}
			}

			const userList = createUserList;

			return {
				code: 200,
				data: userList,
			};
		},
	},
	//删除用户
	{
		url: '/api/user/delUser',
		method: 'post',
		response: (data: any) => {
			LogSingleton.addToLogData({
				logName: '删除用户',
				url: data.url,
				data: data.body,
			});
			console.log(data.userId);
			createUserList = createUserList.filter((item) => item.userId !== data.body.userId);

			return {
				code: 200,
				data: '操作成功',
			};
		},
	},
	//重置密码
	{
		url: '/api/user/rePass',
		method: 'post',
		response: (data: any) => {
			LogSingleton.addToLogData({
				logName: '新增用户',
				url: data.url,
				data: data.body,
			});
			let body = data.body;
			createUserList.map((item) => {
				if (item.userId == body.userId) {
					item.password = '12345';
				}
			});
			return {
				code: 200,
				data: '操作成功',
			};
		},
	},
	// 获取用户信息接口
	{
		url: '/api/user/info',
		method: 'get',
		response: (request: any) => {
			// 获取请求头携带的 token
			const token = request.headers.token;
			// 查看用户信息数据中是否包含有此 token 的用户
			const checkUser = createUserList.find((item) => item.token === token);
			// 没有就返回失败信息
			if (!checkUser) {
				return {
					code: 201,
					data: {
						message: '获取用户信息失败',
					},
				};
			}
			// 有就返回成功信息
			return {
				code: 200,
				data: {
					checkUser,
				},
			};
		},
	},
];
